const init = {
  chatData: [
    // {
    //   username: "张三",
    //   msgdata: "天王盖地虎",
    // },
  ],
  newMsg: {}, //新消息
  connected: false, //链接状态
  connectOption: {}, //链接参数
};
const mqtt = (state = init, action) => {
  switch (action.type) {
    case "SET_MQTT_RCV_MSG":
      let msg = state;
      msg.newMsg = action.data; //实时数据存储

      //如果是聊天数据，那么就更新聊天数组
      if (action.data.topic.indexOf("m2m") != -1) {
        msg.chatData.push({
          username: "abcd",
          msgdata: action.data.payload,
        });
      }
      console.log("msg new state", msg);
      return { ...msg }; //必须是新对象才能触发变化
    case "SET_MQTT_CONNECT_STATE":
      let temp = state;
      temp.connected = action.data;
      return { ...temp }; //必须是新对象才能触发变化
    case "SET_MQTT_CONNECT_OPTION":
      let temp2 = state;
      temp2.connected = action.data;
      return { ...temp2 }; //必须是新对象才能触发变化
    default:
      return state;
  }
};

export default mqtt;
